package com.ruijie.acl.utils;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Set;

import org.richfaces.json.JSONArray;
import org.richfaces.json.JSONException;
import org.richfaces.json.JSONObject;
import org.springframework.util.Assert;

import com.ruijie.acl.AccessGroup;
import com.ruijie.acl.Ace;
import com.ruijie.acl.AclInterface;
import com.ruijie.acl.AclInterfaceView;
import com.ruijie.acl.TimeRange;
import com.ruijie.acl.task.model.AccessGroupInfo;
import com.ruijie.acl.task.model.AceInfo;
import com.ruijie.acl.task.model.AclInterfaceInfo;
import com.ruijie.acl.task.model.TimeRangeInfo;
import com.ruijie.acl.type.EthernetTypeEnum;
import com.ruijie.acl.web.config.Constants;
import com.ruijie.emp.task.util.TaskUtil;
import com.ruijie.foundation.utils.StringUtils;
import com.ruijie.acl.type.EthernetProtocolTypeEnum;

public class AclConfigUtil {
	
	
	/**
	 * 把ACL组名称字符串转换成要下发的AccessGroup信息
	 * @param entity
	 * @return
	 */
	public static List<AccessGroupInfo> string2AccessGroupInfo(String accessGroupNames){
		List<AccessGroupInfo> accessGroupInfoList = new ArrayList<AccessGroupInfo>();
		if (!AclUtil.empty(accessGroupNames)){
			String[] arrAccessGroupName = accessGroupNames.split(";");
			for(String accessGroupName : arrAccessGroupName) {
				if (!AclUtil.empty(accessGroupName)){
					String[] arrTemp = accessGroupName.split(",");
					if (arrTemp.length == 2){
						AccessGroupInfo accessGroupInfo = new AccessGroupInfo();
						accessGroupInfo.setName(AclUtil.unescapeSpecialString(arrTemp[0]));
						accessGroupInfo.setAclType(arrTemp[1]);
						accessGroupInfoList.add(accessGroupInfo);
					}
				}
			}
		}
		return accessGroupInfoList;
	}
	
	/**
	 * 把数据库中取得的ACE的Entity列表转换成ACE信息列表
	 * 
	 * @param  aceList ACE的Entity列表
	 * @return ACE信息列表
	 */
	public static List<AceInfo> ace2AceInfo(List<Ace> aceList){
		List<AceInfo> aceInfoList = new ArrayList<AceInfo>();
		if (aceList != null && aceList.size() > 0){
			for(Ace ace : aceList){
				if(ace!=null){
					aceInfoList.add(ace2AceInfo(ace));
				}
			}
		}
		return aceInfoList;
	}
	
	/**
	 * 把从数据库中取得的ACE的Entity转换成ACE信息
	 * 
	 * @param AceInfo
	 *            P信息
	 * @return ACE的Entity 
	 */
	public static AceInfo ace2AceInfo(Ace ace){
		AceInfo aceInfo = new AceInfo();
		//ace.setAceNum(Integer.parseInt(aceInfo.getAceNum()));
		aceInfo.setAceAction(ace.getActionType());
		aceInfo.setDesAddr(ace.getDesAddr());
		// 00:1e:4f:d6:9a:26 --> 001e.4fd6.9a26
	    // 00-1e-4f-d6-9a-26 --> 001e.4fd6.9a26
		if(ace.getDesMacAddr()!=null){
			aceInfo.setDesMacAddr(TaskUtil.transDeviceMac(ace.getDesMacAddr()));
		}
		aceInfo.setDesMacMatchType(ace.getDesMacMatchType());
		aceInfo.setDesMatchType(ace.getDesMatchType());
		aceInfo.setDesWildcard(ace.getDesWildcard());
		if(StringUtils.isNullOrEmpty(ace.getProtocolType()) && !StringUtils.isNullOrEmpty(ace.getEthernetType())){
			//设置aceInfo的以太网协议类型，处理x25->x.25转换
			aceInfo.setProtocolType(EthernetTypeEnum.valueOf(ace.getEthernetType()).value());
			if (EthernetTypeEnum.etype_any.toString().equals(aceInfo.getProtocolType())){
				aceInfo.setProtocolType(null);
			}
		}else if(!StringUtils.isNullOrEmpty(ace.getProtocolType())){
			aceInfo.setProtocolType(EthernetProtocolTypeEnum.valueOf(ace.getProtocolType()).value());
		}		
		aceInfo.setSourceAddr(ace.getSourceAddr());
		if(ace.getSourceMacAddr()!=null){
			aceInfo.setSourceMacAddr(TaskUtil.transDeviceMac(ace.getSourceMacAddr()));
		}
		aceInfo.setSourceMacMatchType(ace.getSourceMacMatchType());
		aceInfo.setSourceMatchType(ace.getSourceMatchType());
		aceInfo.setSourceWildcard(ace.getSourceWildcard());
		aceInfo.setVlanNum(ace.getVlanNum());
		aceInfo.setTimeRangeName(ace.getTimeRangeName());
		if (ace.getSourcePort() != null){
			aceInfo.setSourcePort(ace.getSourcePort().toString());
		}
		if (ace.getDesPort() != null){
			aceInfo.setDesPort(ace.getDesPort().toString());
		}
		return aceInfo;
	}
	

	
	/**
	 * 把从数据库中取得TimeRange Entity列表转换成TimeRangeInfo信息列表
	 * @param entity
	 * @return
	 */
	public static List<TimeRangeInfo> timeRangeList2TimeRangeInfoList(List<TimeRange> entityList) {
		List<TimeRangeInfo> list = new ArrayList<TimeRangeInfo>();
		if (entityList != null && entityList.size() > 0){
			for(TimeRange info : entityList) {
				list.add(timeRange2TimeRangeInfo(info));
			}
		}
		return list;
	}
	
	/**
	 * 把名称字符串转换成TimeRangeInfo信息列表
	 * @param entity
	 * @return
	 */
	public static List<TimeRangeInfo> string2TimeRangeInfoList(String timeRangeNames) {
		List<TimeRangeInfo> list = new ArrayList<TimeRangeInfo>();
		if (!AclUtil.empty(timeRangeNames)){
			String[] arrTimeRangeName = timeRangeNames.split(",");
			for(String timeRangeName : arrTimeRangeName) {
				if (!AclUtil.empty(timeRangeNames)){
					TimeRangeInfo timeRangeInfo = new TimeRangeInfo();
					timeRangeInfo.setName(AclUtil.unescapeSpecialString(timeRangeName));
					list.add(timeRangeInfo);
				}
			}
			
		}
		return list;
	}
	
	/**
	 * 把从数据库中取得的TimeRange的Entity转换成TimeRange信息
	 * @param entity
	 * @return
	 */
	public static TimeRangeInfo timeRange2TimeRangeInfo(TimeRange entity) {
		Assert.notNull(entity);
		TimeRangeInfo timeRangeInfo = new TimeRangeInfo();
		timeRangeInfo.setName(entity.getName());
		timeRangeInfo.setAbsoluteStartDate(entity.getStartDate());
		timeRangeInfo.setAbsoluteEndDate(entity.getEndDate());
		timeRangeInfo.setTimeInfoList(timeInfoList2TimeInfoList(entity.getTimeInfos()));
		return timeRangeInfo;
	}

	/**
	 * 把数据库TimeInfo信息列表转换成下发设备时用的TimeInfo信息列表
	 * @param entity
	 * @return
	 */
	public static List<com.ruijie.acl.task.model.TimeInfo> timeInfoList2TimeInfoList(Set<com.ruijie.acl.TimeInfo> entitySet) {
		List<com.ruijie.acl.task.model.TimeInfo> entityList = new ArrayList<com.ruijie.acl.task.model.TimeInfo>();
		if (entitySet != null && entitySet.size() > 0){
			for(com.ruijie.acl.TimeInfo info : entitySet) {
				entityList.add(timeInfo2TimeInfo(info));
			}
		}
		return entityList;
	}
	
	/**
	 * 把数据库中取得的TimeInfo的Entity转换成TimeInfo信息
	 * @param entity
	 * @return
	 */
	public static com.ruijie.acl.task.model.TimeInfo timeInfo2TimeInfo(com.ruijie.acl.TimeInfo entity) {
		Assert.notNull(entity);
		com.ruijie.acl.task.model.TimeInfo timeInfo = new com.ruijie.acl.task.model.TimeInfo();
		timeInfo.setStartTime(entity.getStarttime());
		timeInfo.setEndTime(entity.getEndtime());
		timeInfo.setPeriodic(entity.getPeriodic());
		return timeInfo;
	}
	
	/**
	 * 把数据库中取得的AclInterface的Entity转换成AclInterface信息
	 * @param entity
	 * @return
	 */
	public static AclInterfaceInfo aclInterface2AclInterfaceInfo(AclInterface entity) {
		Assert.notNull(entity);
		AclInterfaceInfo aclInterfaceInfo = new AclInterfaceInfo();
		aclInterfaceInfo.setIfName(entity.getDeviceInterface().getName());
		aclInterfaceInfo.setInAccessGroupName(entity.getOutAccessGroupName());
		aclInterfaceInfo.setOutAccessGroupName(entity.getInAccessGroupName());
		return aclInterfaceInfo;
	}
	
	
	/**
	 * 把数据库中取得的AclInterface的Entity转换成AclInterface信息
	 * @param entity
	 * @return
	 */
	public static List<AclInterfaceInfo> aclInterface2AclInterfaceInfoList(List<AclInterface> entity) {
		Assert.notNull(entity);
		List<AclInterfaceInfo> aclInterfaceInfos = new ArrayList<AclInterfaceInfo>();
		for(AclInterface aclInterface:entity){
			AclInterfaceInfo aclInterfaceInfo = new AclInterfaceInfo();
			aclInterfaceInfo.setIfName(aclInterface.getDeviceInterface().getName());
			aclInterfaceInfo.setInAccessGroupName(aclInterface.getInAccessGroupName());
			aclInterfaceInfo.setOutAccessGroupName(aclInterface.getOutAccessGroupName());
			aclInterfaceInfos.add(aclInterfaceInfo);
		}
		return aclInterfaceInfos;
	}
	
	
	
	/**
	 * interfaceView转成List<AccessGroup>
	 * @param interfaceView
	 * @return
	 */
	public static List<AccessGroup> getAccessGroup(AclInterfaceView interfaceView,String inorout){
		List<AccessGroup> accessGroupList = new ArrayList<AccessGroup>();
		if("In".equals(inorout)){
			accessGroupList.add(interfaceView.getInAccessGroup());
		}else{
			accessGroupList.add(interfaceView.getOutAccessGroup());
		}
		return accessGroupList;
	}
	
	/**
	 * interfaceView转成List<AclInterfaceInfo>
	 * @param interfaceView
	 * @return
	 */
	public static List<AclInterfaceInfo> getInterfaceInfos(AclInterfaceView interfaceView,String inorout){
		List<AclInterfaceInfo> interfaceInfoList = new ArrayList<AclInterfaceInfo>();
		AclInterfaceInfo aclInterfaceInfo = new AclInterfaceInfo();
		aclInterfaceInfo.setIfName(interfaceView.getName());
		if("In".equals(inorout)){
			aclInterfaceInfo.setInAccessGroupName(interfaceView.getInAccessGroupName());
		}else{
			aclInterfaceInfo.setOutAccessGroupName(interfaceView.getOutAccessGroupName());
		}
		interfaceInfoList.add(aclInterfaceInfo);
		return interfaceInfoList;
	}
	
	/**
	 * 把数据库中取得的AclInterface的Entity列表转换成AclInterface信息列表
	 * @param entity
	 * @return
	 */
	public static List<AclInterfaceInfo> aclInterfaceList2AclInterfaceInfoList(List<AclInterface> entityList,AccessGroup accessGroup) {
		List<AclInterfaceInfo> resultList = new ArrayList<AclInterfaceInfo>();
		if (entityList != null && entityList.size() >0){
			for(AclInterface aclInterface : entityList){
				AclInterfaceInfo aclInterfaceInfo = new AclInterfaceInfo();
				String inputName = aclInterface.getInAccessGroupName();
				String outputName = aclInterface.getOutAccessGroupName();
				if(accessGroup.getName().equals(inputName)&&
						accessGroup.getName().equals(outputName)){
					aclInterfaceInfo.setInAccessGroupName(inputName);
					aclInterfaceInfo.setOutAccessGroupName(outputName);
				}else if(accessGroup.getName().equals(inputName)){
					aclInterfaceInfo.setInAccessGroupName(inputName);
				}else{
					aclInterfaceInfo.setOutAccessGroupName(outputName);
				}
				aclInterfaceInfo.setIfName(aclInterface.getDeviceInterface().getName());
				
				resultList.add(aclInterfaceInfo);
			}
		}
		return resultList;
	}
	
	/**
	 * 分隔同个AclInterface的不同组，如果一个
	 * AclInterface下的in，out的acl组不同则变为两个AclInterface
	 * @param entityList
	 * @return
	 */
	public static List<AclInterface> converAclInterfaceList(List<AclInterface> entityList){
		List<AclInterface> resultList = new ArrayList<AclInterface>();
		for(AclInterface aclInterface:entityList){
			AclInterface newAclInterface = new AclInterface();
			String inputName = aclInterface.getInAccessGroupName();
			String outputName = aclInterface.getOutAccessGroupName();
			newAclInterface.setDevice(aclInterface.getDevice());
			newAclInterface.setDeviceInterface(aclInterface.getDeviceInterface());
			if(!StringUtils.isNullOrEmpty(inputName) && !StringUtils.isNullOrEmpty(outputName)){
				if(inputName.equals(outputName)){/*InOut组相同*/
					newAclInterface.setInAccessGroup(aclInterface.getInAccessGroup());					
					newAclInterface.setInAccessGroupName(inputName);
					newAclInterface.setOutAccessGroup(aclInterface.getOutAccessGroup());
					newAclInterface.setOutAccessGroupName(outputName);
					resultList.add(newAclInterface);
				}else{/*InOut组不同*/
					newAclInterface.setInAccessGroup(aclInterface.getInAccessGroup());					
					newAclInterface.setInAccessGroupName(inputName);
					resultList.add(newAclInterface);
					newAclInterface = new AclInterface();
					newAclInterface.setDevice(aclInterface.getDevice());
					newAclInterface.setDeviceInterface(aclInterface.getDeviceInterface());
					newAclInterface.setOutAccessGroup(aclInterface.getOutAccessGroup());
					newAclInterface.setOutAccessGroupName(outputName);
					resultList.add(newAclInterface);
				}
			}else if(!StringUtils.isNullOrEmpty(inputName)){
				newAclInterface.setInAccessGroup(aclInterface.getInAccessGroup());					
				newAclInterface.setInAccessGroupName(inputName);
				resultList.add(newAclInterface);
			}else{
				newAclInterface.setOutAccessGroup(aclInterface.getOutAccessGroup());
				newAclInterface.setOutAccessGroupName(outputName);
				resultList.add(newAclInterface);
			}
		}
		return resultList;
	}
	
	/**
	 * 把列表转换成时间段名称字符串
	 * @param timeRangeInfoList 时间段列表
	 * @return 时间段名称字符串
	 */
	public static String convertTimeRangeNames(List<TimeRangeInfo> timeRangeInfoList){
		String result = "";
		if (timeRangeInfoList != null && timeRangeInfoList.size() > 0){
			for(TimeRangeInfo timeRangeInfo : timeRangeInfoList){
				result += "," + timeRangeInfo.getName();
			}
		}
		
		if (!AclUtil.empty(result)){
			result = result.substring(1);
		}
		return result;
	}
	
	/**
	 * 把列表转换成组名称字符串
	 * @param accessGroupInfoList 组列表
	 * @return 组名称字符串
	 */
	public static String convertAccessGroupNames(List<AccessGroupInfo> accessGroupInfoList){
		String result = "";
		if (accessGroupInfoList != null && accessGroupInfoList.size() > 0){
			for(AccessGroupInfo accessGroupInfo : accessGroupInfoList){
				result += "," + accessGroupInfo.getName();
			}
		}
		
		if (!AclUtil.empty(result)){
			result = result.substring(1);
		}
		return result;
	}
	
	public static String convertInterfaceStrings(List<AclInterfaceInfo> aclIntefaceInfoList){
		if (aclIntefaceInfoList != null && aclIntefaceInfoList.size() > 0){
			JSONArray jsonArrMain = new JSONArray();
			for(AclInterfaceInfo aclInterfaceInfo : aclIntefaceInfoList){
				JSONArray jsonArr = new JSONArray();
				jsonArr.put(aclInterfaceInfo.getIfName());
				// 如果下发In的组
				if (!AclUtil.empty(aclInterfaceInfo.getInAccessGroupName())){
					String direction = "In";
					// 如果有下发Out的组
					if (!AclUtil.empty(aclInterfaceInfo.getOutAccessGroupName())){
						// 如果下发的In和Out组一致
						if (aclInterfaceInfo.getInAccessGroupName().equals(aclInterfaceInfo.getOutAccessGroupName())){
							direction = "InOut";
						}
						// 如果不一致，则新增一条记录，分开显示In和Out
						else{
							JSONArray jsonOutArr = new JSONArray();
							jsonOutArr.put(aclInterfaceInfo.getIfName());
							jsonOutArr.put("Out");
							jsonOutArr.put(aclInterfaceInfo.getOutAccessGroupName());
							jsonArrMain.put(jsonOutArr);
						}
					}
					jsonArr.put(direction);
					jsonArr.put(aclInterfaceInfo.getInAccessGroupName());
					jsonArrMain.put(jsonArr);
				}else if (!AclUtil.empty(aclInterfaceInfo.getOutAccessGroupName())){
					jsonArr.put("Out");
					jsonArr.put(aclInterfaceInfo.getOutAccessGroupName());
					jsonArrMain.put(jsonArr);
				}
			}
			JSONObject jsonObj = new JSONObject();
			try {
				jsonObj.put(Constants.ACL_DEPLOY_PLAN_ITEM_PARAM_NAME_INTERFACES, jsonArrMain);
				return jsonObj.toString();
			} catch (JSONException e) {
				//e.printStackTrace();
			}
		}
		return "";
	}

	public static List<AclInterfaceInfo> convertInterfaces(String values){
		List<AclInterfaceInfo> aclInterfaceInfoList = new ArrayList<AclInterfaceInfo>();
		if (!AclUtil.empty(values)){
			try {
				JSONObject jsonObj = new JSONObject(values);
				JSONArray jsonArrMain = jsonObj.getJSONArray(Constants.ACL_DEPLOY_PLAN_ITEM_PARAM_NAME_INTERFACES);
				for(int i = 0;i< jsonArrMain.length();i++){
					JSONArray jsonArr = (JSONArray)jsonArrMain.get(i);
					AclInterfaceInfo aclInterfaceInfo = new AclInterfaceInfo();
					// 取得接口名称
					aclInterfaceInfo.setIfName(jsonArr.getString(0));
					String direction = jsonArr.getString(1);
					if (direction.equals("In")){
						aclInterfaceInfo.setInAccessGroupName(jsonArr.getString(2));
					}else if (direction.equals("Out")){
						aclInterfaceInfo.setOutAccessGroupName(jsonArr.getString(2));
					}else if (direction.equals("InOut")){
						aclInterfaceInfo.setInAccessGroupName(jsonArr.getString(2));
						aclInterfaceInfo.setOutAccessGroupName(jsonArr.getString(2));
					}
					aclInterfaceInfoList.add(aclInterfaceInfo);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		return aclInterfaceInfoList;
	}
	
	/**
	 * 取得绝对时间信息的下发字符串
	 * @param date 日期
	 * @param flag 月份的状态
	 * @return
	 */
	public static String getAbsoluteDateString(Date date,boolean flag){
		String result = "";
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		String month = "";
		// 如果为true，则月份为January, February
		if (flag){
			SimpleDateFormat sdf = new SimpleDateFormat("MMMM",Locale.ENGLISH);
			month = sdf.format(date);
			month = month.substring(0,3);
		}
		// 如果为false，则月份为01,02
		else{
			month = Integer.toString((calendar.get(Calendar.MONTH) + 1));
		}
		result += calendar.get(Calendar.HOUR_OF_DAY) + ":" + calendar.get(Calendar.MINUTE)
					+ " " + calendar.get(Calendar.DAY_OF_MONTH) + " " + month
					+ " " + calendar.get(Calendar.YEAR);
		return result;
	}
}
